//
//  WEXTabBar.m
//  microBlog_oc
//
//  Created by apple on 16/4/17.
//  Copyright © 2016年 Hill. All rights reserved.
//

#import "WEXTabBar.h"
#import "UIView+WEXFrame.h"

@interface WEXTabBar()

@property (nonatomic, strong) UIButton *addButton;

@end

@implementation WEXTabBar

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    
    UIButton *addButton = [[UIButton alloc] init];
    
    self.addButton = addButton;
    
//    添加一个点击方法
    [addButton addTarget:self action:@selector(didClickAddButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [addButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    
    [addButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    [addButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    
    [addButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [addButton sizeToFit];
    
    [self addSubview:addButton];
}


# pragma mark -
# pragma mark 设置子控件位置
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
//    设置添加按钮中心
    self.addButton.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
    
    CGFloat itemWidth = self.frame.size.width / 5;
    int index = 0;
    
//    遍历子控件
    for (UIView *view in self.subviews) {
        
//        判断子控件类型是否为 UITabBarButton
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            view.width = itemWidth;
            
            view.x = index * itemWidth;
            
            index++;
            
//            判断 是否 到第三个子控件
            if (index == 2) {
                
                index++;
            }
        }
        
    }
}



# pragma mark -
# pragma mark 按钮点击方法
- (void)didClickAddButton:(UIButton *)button {
    
    if ([self.delegates respondsToSelector:@selector(tabBar)]) {
        [self.delegates tabBar:self];
    }
    
}

@end
